namespace SugarTank.Mobile.Helpers
{
    using System;

    public static class EventHandlerExtensions
    {
        /// <summary>
        /// Fires the event.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="eventHandler">The event handler.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="createEventArgs">The create event args.</param>
        public static void FireEvent<T>(this EventHandler<T> eventHandler, object sender, Func<T> createEventArgs)
            where T : EventArgs
        {
            EventHandler<T> handler = eventHandler;

            if (handler != null)
            {
                handler(sender, createEventArgs());
            }
        }

        /// <summary>
        /// Fires the event.
        /// </summary>
        /// <param name="eventHandler">The event handler.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="createEventArgs">The create event args.</param>
        public static void FireEvent(this EventHandler eventHandler, object sender, Func<EventArgs> createEventArgs)
        {
            EventHandler handler = eventHandler;

            if (handler != null)
            {
                handler(sender, createEventArgs());
            }
        }
    }
}